# -*- coding: utf-8 -*-

import datetime
import traceback

from utils.db_utils import DbConn
from utils.logger import Md_Debug
from app.manage.recourseManage.common.manage_sql import ResourceManageSql
from utils.backcode import ImportError

class RecourseEditDB(object):
    def __init__(self):
        self.db = DbConn()
        self.nowtime = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")

    def serialize(self,body, params,null_params=None):
        """
        :param body: 请求体
        :param params: 需要解析的参数
        :return: 如果成功返回解析的[数据,空字符],如果不成功返回[None,错误的字段]
        """
        data = []
        for param in params:
            value = body.get(param, None)
            data.append(value)
            if (value == None):
                if null_params!=None:
                    if param not in null_params:
                        return [None, param]
                else:
                    return [None, param]
        return [data, ""]
    
    def add_recourse(self, body):
        ret = dict()
        params = ['applicant_name', 'station', 'tel', 'preliminary_result', 'preliminary_opinion',
                  'reexamination_result', 'reexamination_opinion', 'final_result', 'final_opinion', 'appointment']
        null_params = ['preliminary_result', 'preliminary_opinion',
                  'reexamination_result', 'reexamination_opinion', 'final_result', 'final_opinion']
        try:
            data = self.serialize(body, params,null_params)
            if data[0] == None:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "%s is None" % data[1]
            else:
                # 获得序列化后的数据
                params = data[0]
                ret["response_data"] = self.db.fetch_one(ResourceManageSql.ADD_RECOURSE, params)
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"
        
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret

    def edit_recourse(self, body):
        ret = dict()
        params = ['applicant_name', 'station', 'tel', 'preliminary_result', 'preliminary_opinion',
                  'reexamination_result', 'reexamination_opinion', 'final_result', 'final_opinion', 'appointment','id']
        null_params = ['preliminary_result', 'preliminary_opinion',
                       'reexamination_result', 'reexamination_opinion', 'final_result', 'final_opinion']
        try:
            data = self.serialize(body, params,null_params)
            if data[0] is None:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "%s is None" % data[1]
            else:
                params = data[0]
                self.db.commit(ResourceManageSql.EDIT_RECOURSE, params)
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"
    
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
    
    def del_recourse(self, body):
        #TODO删除人力资源应该有相应的用户权限
        ret = dict()
        params = ['id']
        try:
            data = self.serialize(body, params)
            if data[0] is None:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "%s is None" % data[1]
            else:
                params = data[0]
                self.db.commit(ResourceManageSql.DEL_RECOURSE, params)
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"

        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret